<?php
namespace payClass\pay;

/**
 * 商户配置
 */
class Config
{

	// 当前文件夹名
	public static $folder;
	// 商户号
    public static $merchant = '200755637';
    // 密钥
    public static $key      = '4lt2dffttfdg33r4ld433nv7owcqb7q4';
    // 同步回调（通知）地址
    public static $merUrl;
    // 异步回调（通知）地址
    public static $notifyUrl;
    // 代付异步回调（通知）地址
    public static $paymentNotifyUrl;
    // 提交地址
    public static $subUrl   = 'https://pay.mypays.in/';

	public function __construct()
	{
		self::$folder    = substr(__DIR__, strrpos(__DIR__, '\\')+1);

		self::$merUrl    = 'http://'.$_SERVER['HTTP_HOST'];

		self::$notifyUrl = self::$merUrl . DIRECTORY_SEPARATOR . 'payClass' . self::$folder . DIRECTORY_SEPARATOR . 'sycNotice.php';

        self::$paymentNotifyUrl = self::$merUrl . DIRECTORY_SEPARATOR . 'payClass' . self::$folder . DIRECTORY_SEPARATOR . 'paymentNotice.php';
	}

	public static function getSignMsg($data=[])//签名
    {
        if (!$data) return false;

		$signStr = '';
        // 以参数名的字典顺序排序
		ksort($data);
        // 过滤空值
        $array = array_filter($data);

		foreach ($array as $key => $value) {

			$signStr .= $key . "=" . $value . "&";

		}

		$signStr .= 'key=' . self::$key;

		$sign    = md5($signStr);

		return strtolower($sign);
	}

	//发送请求
	public function setHttpRequest($url, $params = array(), $method = 'POST', $headers = '')
	{
        $ci = curl_init();

        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);

        curl_setopt($ci, CURLOPT_TIMEOUT, 30);

        curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method);

        curl_setopt($ci, CURLOPT_URL, $url);

        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);

        curl_setopt($ci, CURLOPT_HEADER, FALSE);

		curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查

		curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在

        if ($headers != '') {

            curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);

        } else {

            curl_setopt($ci, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

        }

        switch (strtoupper($method)) {
            case 'POST':

                curl_setopt($ci, CURLOPT_POST, TRUE);

                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);

                break;

            case 'PUT':

                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT');

                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);

                break;

            case 'PATCH':

                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PATCH');

                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);

                break;

            case 'DELETE':

                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');

                curl_setopt($ci, CURLOPT_POSTFIELDS, $params);

                break;

            default:

                curl_setopt($ci, CURLOPT_HTTPGET, TRUE);

                break;
        }

        $response = curl_exec($ci);

        $httpCode = curl_getinfo($ci);

        curl_close($ci);

        return $response;
    }
}